package com.lin.generate.common.utill;


import com.lin.generate.common.enums.SystemEnums;
import com.lin.generate.common.exception.AssertsException;

import java.util.Collection;

/**
 * @describe:
 * @Author: SJXN_LZHAO
 * @Date: 2021/12/28 14:31
 */
public class Asserts {

    public static void check(boolean expression, String message) {
        if (!expression) {
            throw new AssertsException(SystemEnums.ASSERT_ERROR, message);
        }
    }

    public static void check(boolean expression, String message, Object... args) {
        if (!expression) {
            throw new AssertsException(SystemEnums.ASSERT_ERROR, String.format(message, args));
        }
    }

    public static void check(boolean expression, String message, Object arg) {
        if (!expression) {
            throw new AssertsException(SystemEnums.ASSERT_ERROR, String.format(message, arg));
        }
    }

    public static void notNull(Object object, String name) {
        if (object == null) {
            throw new AssertsException(SystemEnums.ASSERT_ERROR, name + " is null");
        }
    }

    public static void isNull(Object object, String name) {
        if (object != null) {
            throw new AssertsException(SystemEnums.ASSERT_ERROR, name + " is null");
        }
    }

    public static void notEmpty(Collection collection, String name) {
        notNull(collection, name);
        check(!collection.isEmpty(), StrBuilderUtil.concat(name," is empty"));
    }

    public static void noNullElements(Collection collection, String name) {
        notNull(collection, name);
        collection.forEach(col->notNull(col,name));
    }

    public static void notEmpty(CharSequence s, String name) {
        if (s==null || s.length() == 0) {
            throw new AssertsException(SystemEnums.ASSERT_ERROR, name + " is empty");
        }
    }

    public static void notBlank(CharSequence s, String name) {
        if (s==null || s.length() == 0) {
            throw new AssertsException(SystemEnums.ASSERT_ERROR, name + " is blank");
        }
    }

    public static void noNullElements(Object[] array, String name) {
        notNull(array, name);
        for(int i = 0; i < array.length; ++i) {
            notNull(array[i],name);
        }
    }
}
